<template>
    <div class="container">

        <div class="header">
            <div class="h-left">
                <img src="" alt="">
                <span>{{sysTitle}}</span>
            </div>
            <div class="h-right">
                <div class="avatar-box">
                    <i class="iconfont icon-yonghu"></i>
                </div>
                    <el-popover
                            placement="bottom"
                            trigger="click"
                            popper-class="popover-wrap"
                            popper-style="padding:6px 0;"
                    >
                        <template #default>
                            <ul class="user-opt">
                                <li class="opt-item" @click="handleShow">修改密码</li>
                                <li class="opt-item" @click="logout">退出登录
                                    <!--<router-link to="/login"></router-link>-->
                                </li>
                            </ul>
                        </template>
                        <template #reference>
                            <div class="user-box">
                            <span>{{store.state.user.userName}}</span>
                            <i class="iconfont icon-xiajiantou"></i>
                            </div>
                        </template>
                    </el-popover>
            </div>
        </div>
        <div class="main">
            <div class="main-left" :class="{width0: closeSideBar}">
                <div class="sidebar-close" :class="{'drop-down': closeSideBar}" @click="closeSideBar = !closeSideBar">
                    <i class="iconfont icon-xiajiantou"></i>
                </div>
                <sidebar/>
            </div>
            <div class="main-center">

                <tabs></tabs>
                <div class="content">
                    <router-view v-slot="{ Component }" v-if="isRouterAlive" :key="router.path">
                        <transition name="fade" mode="out-in">
                        <keep-alive :max="10">
                            <component :is="Component" />
                        </keep-alive>
                        </transition>
                    </router-view>
                </div>
            </div>
        </div>
    </div>
    <!--修改密码-->
    <m-dialog-form ref="mDialogRef" :data="mDialog.data" v-model:show="mDialogShow" :option="mDialog" @confirm="modifyPwd" ></m-dialog-form>
</template>

<script setup>
    import { provide, ref, nextTick,reactive} from 'vue';
    import {useRouter} from 'vue-router';
    import {useStore} from 'vuex';

    import {getUser,editUser} from "../../request/api/basicData";

    import MDialogForm from '@/components/DialogForm/DialogForm';

    const router = useRouter(),
      store = useStore(),
      sysTitle = process.env.NODE_ENV === 'development' ? process.env.VUE_APP_SYSTITLE : window.webConfig.webSystemTitle;
    /*组件*/
    import Tabs from './tabs'
    //左侧菜单
    import Sidebar from './sidebar'

    let isRouterAlive = ref(true),
      closeSideBar = ref(false)
    // let user_name = store

    let mDialog = reactive({
        title: '修改密码',
        fieldList: [
          {field_id: 'old_password',label: '原密码',type: 'password',col: 3 ,required: true },
          {field_id: 'new_password',label: '新密码',type: 'password',col: 3 ,required: true },
          {field_id: 'confirm_password',label: '确认密码',type: 'password',col: 3 ,required: true },
        ],
        show: false,
        data: {},
      }),
      mDialogShow = ref(false),
      mDialogRef = ref(null);

    //刷新路由-删除组件
    function refresh() {
      isRouterAlive.value = false;
      nextTick(() => {
        isRouterAlive.value = true
      })
    }

    //退出登录
    const logout = () =>{
      store.dispatch('logout')
      router.push('/login')
    }

    const handleShow = () => {
      mDialogShow.value = true;
      mDialog.data = {};
    }
    //修改密码
    const modifyPwd = (obj) => {
      let user_id = store.state.user.userId,
      {confirm_password,new_password,old_password} = obj.data,
        row_id = 0;
      getUser({user_id}).then(res => {
        if (res.errcode != 0) return;
        let data = res.row[0];
        row_id = data.row_id;
        if (data && data.password != old_password){
          ElMessage({
            type: 'error',
            message: '原密码不正确'
          });
          return
        }
        if (new_password != confirm_password){
          ElMessage({
            type: 'warning',
            message: '新密码与确认密码不一致'
          });
          return
        }
        editUser({row_id,password: new_password}).then(res =>{
          if (res.errcode != 0) return;
          ElMessage({
            type: 'success',
            message: '修改成功'
          });
          mDialogShow.value = false;
        })
      })
    }

    provide('refresh',refresh)

</script>

<style scoped lang="less">
    .container{
        height: 100vh;
        display: flex;
        flex-direction: column;
    }
    .header{
        background: @hd-color;
        height: 68px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 20px;
        color: #fff;
        .h-left{
            font-size: 20px;
        }
        .h-right{
            display: flex;
            align-items: center;
            .avatar-box{
                width: 30px;
                height: 30px;
                border-radius: 30px;
                border: 2px solid #8BB1FF;
                background: #fff;
                margin: 0 14px;
                display: flex;
                align-items: center;
                justify-content: center;
                & > i{
                    color: #8BB1FF;
                }
            }
            .user-box{
                font-size: 14px;
                cursor: pointer;
                & > span{
                    margin-right: 6px;
                }
                & > i{
                    font-size: 12px;
                }
            }
        }
    }
    .main{
        flex: 1;
        display: flex;
        height: 0;
        .main-left{
            height: 100%;
            position: relative;
            transition: all 0.3s ease;
            width: 200px;
            &.width0{
                width: 0;
            }
            .sidebar-close{
                position: absolute;
                top: 50%;
                right: -14px;
                height: 40px;
                width: 14px;
                display: flex;
                align-items: center;
                justify-content: center;
                background: #1E2140;
                border-radius: 0 3px 3px 0;
                cursor: pointer;
                & > .icon-xiajiantou{
                    color: #ffffff;
                    font-size: 14px;
                    transform: rotateZ(270deg);
                }
                &.drop-down{
                    & > .icon-xiajiantou{
                        transform: rotateZ(90deg);
                    }
                }
                &:hover{
                    background: #4e5173;
                }
            }
        }
        .main-center{
            flex: 1;
            display: flex;
            flex-direction: column;
            height: 100%;
            width: 0;
        }
        .content{
            flex: 1;
            background: #F0F1F4;
            padding: 20px;
            height: 0;
        }
    }

    .popover-wrap{
        padding: 10px 0;
        .user-opt{

            .opt-item{
                cursor: pointer;
                height: 40px;
                line-height: 40px;
                text-align: center;
                color: #333333;
                &:hover{
                    background: #eff3ff;
                    color: @main-color;
                }
            }
        }
    }

    .fade-enter-from{   /* 进入时的透明度为0 和 刚开始进入时的原始位置通过active透明度渐渐变为1 */
        opacity: 0;
        transform: translateX(-20px);
    }

    .fade-enter-to{   /*定义进入完成后的位置 和 透明度 */
        transform: translateX(0px);
        opacity: 1;
    }

    .fade-leave-active,.fade-enter-active {
        transition: all 0.4s;
    }

    .fade-leave-from { /* 页面离开时一开始的css样式,离开后为leave-to，经过active渐渐透明 */
        transform: translateX(0%);
        opacity: 1;
    }

    .fade-leave-to{   /* 这个是离开后的透明度通过下面的active阶段渐渐变为0 */
        transform: translateX(20px);
        opacity: 0;
    }

</style>
